/*
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.scanner.utility;

import java.io.File;
import java.util.Optional;

/**
 * @title PathUtils.java
 * @package com.hyts.apidoc.utility
 * @description TODO
 * @see TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com
 * @date 2018年4月18日
 * @version V1.0
 */
public class PathUtils {

	/**
	 * <b>package2Path</b>
	 * <p>package包路径转换为path文件路径</p>
	 * @param packagePath
	 * @return 转换后的文件路径
	 * @exception
	 */
	public static String package2Path(String packagePath) {
		return StringUtils.isNotBlank(packagePath)?packagePath.replace(".",File.separator):"";
	}

	/**
	 * <b>package2Path</b>
	 * <p>package包路径转换为path文件路径</p>
	 * @param packagePath
	 * @return 转换后的文件路径
	 * @exception
	 */
	public static String package2Path2(String packagePath) {
		return StringUtils.isNotBlank(packagePath)?packagePath.replace(".","/"):"";
	}

	/**
	 * <b>path2Package</b>
	 * <p>path路径转换为package文件路径</p>
	 * @param path
	 * @return 转换后的包路径
	 * @exception
	 */
	public static String path2Package(String path) {
		return StringUtils.isNotBlank(path)?path.replace(File.separator,"."):"";
	}

	/**
	 * <b>path2Package</b>
	 * <p>path路径转换为package文件路径</p>
	 * @param path
	 * @return 转换后的包路径
	 * @exception
	 */
	public static String path2Package2(String path) {
		return StringUtils.isNotBlank(path)?path.replace("/","."):"";
	}

	/**
	 * <b>classPath2FilePath</b>
	 * <p>classpath路径转为实际文件路径</p>
	 * @param path
	 * @return
	 * @exception
	 */
	public static String classPath2FilePath(String path) {
		return Thread.currentThread().getContextClassLoader().getResource(path).getPath();
	}

	/**
	 * <b>package2FilePath</b>
	 * <p>package路径转为实际文件路径</p>
	 * @param packagePath
	 * @return
	 * @exception
	 */
	public static String package2FilePath(String packagePath) {
		String packagePath2 = packagePath.replace(".", "/");
		return Optional.ofNullable(Thread.currentThread().getContextClassLoader().
				getResource(packagePath2)).orElse(PathUtils.class.getResource(packagePath2)).getPath();
	}

}
